package org.brisling.satisfaction.service;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.List;

import org.brisling.common.ConstSysParam;
import org.brisling.satisfaction.domain.SatisfactionEvaluateResult;
import org.brisling.satisfaction.service.base.BaseSatisfactionService;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.ObjectMapper;

@RestController
@RequestMapping("/satisfactionEvaluateResult")


/**
 * 满意度答题者个人信息service
 * @Author : SherlockMoon
 * @Date   : 2016年8月02日
 * @version: 1.0
 */


public class SatisfactionEvaluateResultService extends BaseSatisfactionService<SatisfactionEvaluateResult>{

	public SatisfactionEvaluateResultService(){
		this.clazz = SatisfactionEvaluateResult.class;
		this.moduleName = ConstSysParam.MODULE_SATISFACTION_EVALUATE_RESULT;	
		this.subSystem = ConstSysParam.SUBSYSTEM_STATISFACTION;
	}
	
	
	//把前台的json数据封装成List<SatisfactionEvaluateResult>
	private List<SatisfactionEvaluateResult> getSatisfactionEvaluateResultList(String body) {
		String _body = null;
		try {
			_body = java.net.URLDecoder.decode(body,"UTF-8");
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		};
		ObjectMapper mapper = new ObjectMapper();
		mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);	
		JavaType _javaType = mapper.getTypeFactory().constructCollectionType(List.class, SatisfactionEvaluateResult.class);
		List<SatisfactionEvaluateResult> list = null;
		try {
			list =  mapper.readValue(_body, _javaType);
		} catch (IOException e1) {
			e1.printStackTrace();
		}	
		return list;
	}

	/**
	 * 
	 * @param body
	 * @param evaluate_id	评价主表id
	 * @return
	 */
	public List<SatisfactionEvaluateResult> saveEvaluateResult(String body, String evaluate_id) {
		List<SatisfactionEvaluateResult> list = getSatisfactionEvaluateResultList(body);
		if (list != null) {
			for (SatisfactionEvaluateResult e : list) {
				e.setEvaluate_id(evaluate_id);
				e.setResult(String.valueOf(6 - Integer.valueOf(e.getScore())));
			}
			if(list!=null && (list.size()>0)){				
					list =(List<SatisfactionEvaluateResult>) baseRepo.save(list);
			}	
		}
		return list;
	}
	
	/**
	 * 保存评价结果明细
	 * @param list		评价结果集合
	 * @param evaluate_id	评价人
	 * @return
	 */
	public List<SatisfactionEvaluateResult> saveResult(List<SatisfactionEvaluateResult> list) {
		
		if(list!=null && (list.size()>0)){				
				list =(List<SatisfactionEvaluateResult>) baseRepo.save(list);
		}	
		
		return list;
	}

	
}


